#!/usr/bin/env /usr/local/python3
# -*- coding: utf-8 -*-

import requests
import cv2

"""
机顶盒点播模拟
"""

__author__ = "hubert"

#1、 http://192.168.241.24:8082/EDS/jsp/AuthenticationURL?UserID=311216957580502&Action=Login
# get 302 跳转，获取302后的http前缀地址
def getAuth(req):
    url_auth = "http://192.168.241.24:8082/EDS/jsp/AuthenticationURL?UserID=311216957580502&Action=Login"
    headers = {"Content-Type":"application/x-www-form-urlencoded", "User-Agent": "Mozilla/5.0 (Linux; Android 4.4.2; UNT401H-0U_IPTV Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.157 Crosswalk/24.53.595.0 Safari/537.36; Resolution(PAL,720P)"}
    res = req.get(url=url_auth, headers=headers)
    return res


#2、 http://192.168.236.6:33200/EPG/jsp/authLoginHWCTC.jsp
# psot
def getHWCTC(pre_url, req):
    url_hwctc = pre_url + '/EPG/jsp/authLoginHWCTC.jsp'
    headers = {"Content-Type":"application/x-www-form-urlencoded", "User-Agent": "Mozilla/5.0 (Linux; Android 4.4.2; UNT401H-0U_IPTV Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.157 Crosswalk/24.53.595.0 Safari/537.36; Resolution(PAL,720P)"}
    data = {"UserID" : "311216957580502","VIP" : ""}
    res = req.post(url=url_hwctc, data=data,headers=headers)
    return res


#3、 http://192.168.236.6:33200/EPG/jsp/ValidAuthenticationHWCTC.jsp
# psot 获取JSESSIONID
def getValidHWCTC(pre_url, req):
    url_hwctc = pre_url + '/EPG/jsp/ValidAuthenticationHWCTC.jsp'
    headers = {"Content-Type":"application/x-www-form-urlencoded", "User-Agent": "Mozilla/5.0 (Linux; Android 4.4.2; UNT401H-0U_IPTV Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.157 Crosswalk/24.53.595.0 Safari/537.36; Resolution(PAL,720P)"}
    data = {"UserID" : "311216957580502",
            "Lang" : "",
            "SupportHD" : "",
            "NetUserID" : "",
            "Authenticator" : "4AFCEDE129EB95F7CCF8924F79441860AD65628C9853214ABD156177A3DCEDF6C5452C4BA93FD46FBE859A938226004B8EAD6A58E197F2271550EF606E7F7FA3C5BEE7FA88AA14EEAA04318E9589533EBA0C58EEB37A473A729D5498B3F3480DCF38EBD0CFF34746124D82569F4BE9C0FF6F46EB1511C369BB78900DCF5B94B4",
            "STBType" : "UNT401H-0U_IPTV",
            "STBVersion" : "",
            "conntype" : "",
            "STBID" : "00340300001897800029748F1BDF3D03",
            "templateName" : "",
            "areaId" : "",
            "userToken" : "BBCDC5260A8A63CCB61DC5C58B5CF009",
            "userGroupId" : "",
            "productPackageId" : "",
            "mac" : "",
            "UserField" : "2",
            "SoftwareVersion" : "",
            "IsSmartStb" : "undefined",
            "desktopId" : "undefined",
            "stbmaker" : "",
            "XMPPCapability" : "",
            "ChipID" : "undefined",
            "VIP" : ""}
    res = req.post(url=url_hwctc, data=data, headers=headers)
    return res


# 4、http://192.168.236.6:33200/EPG/jsp/indexHWCTC.jsp?lang=1&UserID=311216957580502
# 访问首页
def getIndexHw(pre_url, req):
    url_index = pre_url + '/EPG/jsp/ValidAuthenticationHWCTC.jsp'
    headers = {"Content-Type":"application/x-www-form-urlencoded", "User-Agent": "Mozilla/5.0 (Linux; Android 4.4.2; UNT401H-0U_IPTV Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.157 Crosswalk/24.53.595.0 Safari/537.36; Resolution(PAL,720P)"}

    return

# 5、调用获取播放地址接口
# http://192.168.236.6:33200/EPG/jsp/YDHWZhengShi2/en/datajsp/play_getVodPlayUrl.jsp?mediaCode=PROG_WASU00000000000015555736
def getVodUrl():
    return


# 模拟视频播放
def videoCapture():
    url = "rtsp://192.168.248.9:554/7/16/20211028/272648477/272648477.ts?startTime=0&rrsip=192.168.241.26&zoneoffset=480&icpid=hbydhw&accounttype=1&limitflux=-1&limitdur=-1&tenantId=8602&accountinfo=WAJeieFsO%2BdWW2LkNJxEGtiPNR9%2FlRJyDLABQySZuEExDNF0%2F00mdxCt7IV4StUlTDyjwL4MkSjJiAmphM3TzNHG5KIJQFaM%2BUTZIVKdEzHg9BJUQDznUMDs9%2FshmsnCRtb0yDSRNNRK3%2BmmKO0cyOxY3A2qWYPaXIPkYhqC3iU%3Dbe309f823d584c7bcc77927eac3a3ce5%3A20211102105026%2C10008848313450%2C10.110.42.70%2C20211102105026%2CPROG_WASU00000000000015555736%2C10003733884085%2C0.0%2C1%2C0%2C%2C%2C1%2C0%2C%2C%2C1%2C%2CEND&GuardEncType=2&it=H4sIAAAAAAAAAC2PXQuCMBiF_80uh3M17WJXRhDECrRu422-LnE621To36fm5fl44JzBg8bzUXajtSTgRzkZEw3W1p1RrlyiR549WURZnFB2EDRNSLFAJwtGRmtXje0L_SZmMEc_1RplGSo6QaBgjEcDQ-06erPwvXu7VQgW2wAmuNjxhPNduhdkWNwCQjMn5A0hc20PHsuLMysgK7ABSQ-6AYMKWvw_WLmrL-c1P3Jl0ijaAAAA&HBGDMediaType=3&HBGDContentID=PROG_WASU00000000000015555736"
    cap = cv2.VideoCapture(url)  # rtsp://admin:admin@192.168.2.64:554//Streaming/Channels/1
    ret, frame = cap.read()
    while ret:
        ret, frame = cap.read()
        print(cap.get(0))  #cap.get(0) 视频文件的当前位置（播放）以毫秒为单位
        # """
        if ret:
            # 可调整视频窗大小
            cv2.namedWindow("Video_Frame", cv2.WINDOW_NORMAL)
            # 设置视频窗默认大小
            cv2.resizeWindow("Video_Frame", 640, 480);
            # 显示视频窗
            cv2.imshow("Video_Frame", frame)
        #"""
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cv2.destroyAllWindows()
    cap.release()


# 运行
if __name__ == '__main__':
    # 1、AuthenticationURL
    sess = requests.session()
    res_auth = getAuth(sess)
    # print(res_auth.headers)
    # print(res_auth.text)
    preurl = res_auth.url.split("/EPG",1)
    # print(preurl[0])

    # 2、authLoginHWCTC
    res_hwctc = getHWCTC(preurl[0],sess)
    #print(res_hwctc.text)

    # 3、ValidAuthenticationHWCTC
    res_valid_auth = getValidHWCTC(preurl[0],sess)
    print(res_valid_auth.headers)
    print(res_valid_auth.headers["Set-Cookie"])
    # print(res_valid_auth.text)
    print(res_valid_auth.cookies["JSESSIONID"])










